﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace pigs
{
    public class DynamicObject
    {
        public Texture2D texture; // The image for a dynamic objected
        public Vector2 position; // The position of the big relative to the top left corner of the screen
        public bool active;      // The state of the pig
        public Rectangle rectangle; // The verticies of an object's image
        public SpriteEffects effect; // If it should be flipped or anything
        public Color[] colorMap; // The distribution of color for a texture
        public float startY; // The starting y position of a pig

        public float health;  // The hit points of the pig, if this goes to zero the enemy dies
        public float damage;  // The amount of damage the enemy inflicts on the pig
        public int value;   // The amount of score the pig will give to the player

        public string ID; // ID for pigs

        public float speed; // The x speed for a dynamic object

        public DynamicObject(Texture2D texture, Vector2 position)
        {
            this.texture = texture;
            this.position = position;
            this.active = true;
            this.effect = SpriteEffects.None;
            this.startY = position.Y;

            colorMap = new Color[texture.Width * texture.Height];
            texture.GetData(colorMap);

            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
        }

        public virtual void Update(GameTime gameTime) { }

        public virtual Color[] getColorMap()
        {
            return this.colorMap;
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.texture, this.position, null, Color.White, 0f, Vector2.Zero, 1f, effect, 0f);
        }

        public Boolean checkCollision(DynamicObject that)
        {
            // Find the bounds of the rectangle intersection
            int top = Math.Max(this.rectangle.Top, that.rectangle.Top);
            int bottom = Math.Min(this.rectangle.Bottom, that.rectangle.Bottom);
            int left = Math.Max(this.rectangle.Left, that.rectangle.Left);
            int right = Math.Min(this.rectangle.Right, that.rectangle.Right);

            // Check every point within the intersection bounds
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    if (this.colorMap[(x - this.rectangle.Left) + (y - this.rectangle.Top) * this.rectangle.Width].A != 0)
                    {
                        if (that.colorMap[(x - that.rectangle.Left) + (y - that.rectangle.Top) * that.rectangle.Width].A != 0)
                        {
                            return true;
                        }
                    }
                }
            }

            // No intersection found
            return false;
        }

        public int Width
        {
            get { return this.texture.Width; }
        }

        public int Height
        {
            get { return this.texture.Height; }
        }

        public Vector2 center()
        {
            return new Vector2(this.rectangle.Center.X, this.rectangle.Center.Y); ;
        }
        
        /// <summary>
        /// take a given texture, flip it and return it
        /// </summary>
        /// <param name="source">source texture</param>
        /// <param name="vertical">flip it vertically?</param>
        /// <param name="horizontal">flip it horizontally?</param>
        /// <returns></returns>
        public Texture2D Flip(Texture2D source, bool vertical, bool horizontal)
        {
            //create the texture and arrays to store info
            Texture2D flipped = new Texture2D(source.GraphicsDevice, source.Width, source.Height);
            Color[] data = new Color[source.Width * source.Height];
            Color[] flippedData = new Color[data.Length];

            //get data of source image
            source.GetData(data);

            //do the actual flipping
            for (int x = 0; x < source.Width; x++)
                for (int y = 0; y < source.Height; y++)
                {
                    int sourceIndex = 0;
                    if (horizontal)
                        sourceIndex += source.Width - 1 - x;
                    else
                        sourceIndex += x;
                    if (vertical)
                        sourceIndex += (source.Height - 1 - y) * source.Width;
                    else
                        sourceIndex += y * source.Width;
                    flippedData[x + y * source.Width] = data[sourceIndex];
                }

            //set the data and return
            flipped.SetData<Color>(flippedData);
            return flipped;
        }
    }
}
